const { pool, add, edit } = require('./baseModel.js')

exports.add = async (fields) => {
    return await add('address_info', fields)
}

exports.edit = async (fields, where) => {
    return await edit('address_info', fields, where)
}

exports.delete = async (id, uid) => {
    return edit('address_info', { isDel: 1 }, { id, uid })
}

exports.findById = async (id, uid) => {
    let result = await pool.query('select * from address_info where id=? and uid=? and isDel=0', [id, uid])
    return result[0]
}

exports.findByUid = async (uid) => {
    let result = await pool.query('select * from address_info where uid=? and isDel=0', [uid])
    return result[0]
}

exports.findByDefault = async (uid) => {
    let result = await pool.query('select * from address_info where uid=? and isDel=0 and isDefault!=0', [uid])
    return result[0]
}

exports.setDefault = async (uid, id) => {
    // await pool.query('start transaction')
    await pool.query('update address_info set isDefault=0 where id=(select id from (select * from address_info as a where isDel=0 and isDefault=1 and uid=?) as temp)', [uid])
    let result = await edit('address_info', { isDefault: 1 }, { uid, id })
    // await pool.query('commit')
    return result
}